<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="profileObj.photo"
            @click="$refs.iptFile.click()"
          />
          <!-- file 选择框 -->
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell
        title="名称"
        is-link
        :value="profileObj.name"
        @click="nameClickFn"
      />
      <!-- 修改用户名称的对话框 -->
      <van-dialog
        v-model="show"
        title="修改名称"
        show-cancel-button
        :before-close="beforeCloseFn"
      >
        <!-- 输入框 -->
        <input
          type="text"
          v-model.trim="userName"
          input-align="center"
          maxlength="7"
          placeholder="请输入名称"
          v-fofo
        />
      </van-dialog>
      <van-cell title="生日" is-link :value="profileObj.birthday" @click="birthdayFn"/>
      <!-- 修改时间 -->
      <van-popup
        v-model="isShowBirth"
        position="bottom"
        style="height: 50%"
        round
      >
        <!-- 日期选择控件 -->
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          title="选择出生日期"
          :min-date="minDate"
          :max-date="maxDate"
          :show-toolbar="true"
          @cancel="dateCancelFn"
          @confirm="confirmFn"
        />
      </van-popup>
    </van-cell-group>
  </div>
</template>

<script>
import { userProfileAPI, uesrPhotoAPI, updateUserProfileAPI } from '@/api'
import Notify from '@/ui/Notify'
import { formatDate } from '@/utils/date.js'
import { mapMutations } from 'vuex'
export default {
  name: 'UserEdit',
  data () {
    return {
      profileObj: {},
      userName: '',
      show: false,
      isShowBirth: false, // 显示时间选择器
      minDate: new Date(1920, 0, 1), // 最小的可选的日期
      maxDate: new Date(), // 最大的可选日期
      currentDate: new Date() // 当前日期
    }
  },
  async created () {
    const res = await userProfileAPI()
    // console.log(res)
    this.profileObj = res.data.data
  },
  methods: {
    ...mapMutations(['SET_USERPHOTO']),
    //   点击图片 -> input的change事件
    async onFileChange (e) {
      if (e.target.files.length === 0) return // 用户可能没有选图片,就阻止代码往下运行
      //   console.log(e.target.files[0])
      const theFd = new FormData()
      theFd.append('photo', e.target.files[0]) // 请求体里加入一对参数
      const res = await uesrPhotoAPI(theFd)
      //   console.log(res)
      this.profileObj.photo = res.data.data.photo
      this.SET_USERPHOTO(res.data.data.photo)
    },
    nameClickFn () {
      this.show = true
      this.userName = this.profileObj.name
    },
    // 姓名 - 确认框  - 关闭前回调函数
    async beforeCloseFn (action, done) {
      //   console.log(action)
      if (action === 'confirm') {
        // 点的确定
        const reg = /^[a-zA-Z0-9\u4e00-\u9fa5]{1,7}$/
        if (reg.test(this.userName) === true) {
          // 通过
          await updateUserProfileAPI({
            name: this.userName
          })
          this.profileObj.name = this.userName // 更新成功后,回显到页面
          done()
        } else {
          // 没通过验证,给个提示
          Notify({ type: 'warning', message: '只能为1*7位英文中文数字组合' })
          done(false)
        }
      } else {
        // 点的取消
        done() // 让弹窗关闭
      }
    },
    birthdayFn () {
      this.isShowBirth = true
      this.currentDate = new Date(this.profileObj.birthday)
    },
    // 日期选择器  取消事件
    dateCancelFn () {
      this.isShowBirth = false
    },
    // 日期选择器  完成 确认事件
    async confirmFn () {
      await updateUserProfileAPI({
        birthday: formatDate(this.currentDate)
      })
      this.isShowBirth = false
      this.profileObj.birthday = formatDate(this.currentDate)
    }
  }
}
</script>

<style lang="less" scoped>
::v-deep .van-dialog__content {
  text-align: center;
  input {
    padding: 0;
    outline: none;
    border: none;
    text-align: center;
  }
}
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
</style>
